#!/bin/bash

TEMPDIR=/data/audio/COMPIL/ISO

if [ $# -ne 1 ] || ! [ -r $1 ]; then
	echo "make_wavs converts a playlist to wav files"
	echo "usage: $0 <fichier.pls>"
	exit
fi

mkdir -p $TEMPDIR
if [ $? -ne 0 ]; then 
	echo "The $TEMPDIR (\$TEMPDIR) directory isn't usable."
	exit
fi

NBTRACKS=$(echo "$(wc -l < $1)-2" | bc)
FILES=$(cat $1 | tail -n $NBTRACKS | cut -d= -f2)

if [ $(for i in $FILES; do echo $i; done | wc -l) -ne $NBTRACKS ]; then
	echo "fatal: i think there is spaces in filenames, this is forbidden."
	exit
fi

CPT=1
for file in $FILES; do
	if echo $file | grep '3$'; then
		mpg123 -w $TEMPDIR/$(printf "%02d" $CPT)-$(basename $file).wav $file
	else
		oggdec -o $TEMPDIR/$(printf "%02d" $CPT)-$(basename $file).wav $file
	fi
	CPT=$(($CPT+1))
done

